package game;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Toolkit;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import game.User;
/*
 * 主窗口
 * 可以切換游戲和記錄
 * 北邊   菜單
 * 中間   容器可以存放 游戲界面和記錄界面
 */
public class MainJFrame extends JFrame implements ActionListener {
	//菜單欄  菜單 菜單項
	private JMenuBar bar;
	private JMenu menu;
	private JMenuItem item;
	private User user;
	private int screenW = (int) Toolkit.getDefaultToolkit().getScreenSize().width;
	private int screenH = (int) Toolkit.getDefaultToolkit().getScreenSize().height;
	//中間
	private JPanel centerJPanel;
	
	public MainJFrame(User user) {
		this.user =user;
		// 标题
		this.setTitle("打地鼠");
		// 大小
		this.setSize(400,500);
		// 位置
		this.setLocation((screenW-this.getWidth())/2, (screenH-this.getHeight())/2);
		// 是否可扩大
		this.setResizable(false);
		// 关闭窗口
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		// 初始化组件
		initComponet();
		// 是否可视化
		this.setVisible(true);
	}
	private void initComponet() {
		//北邊   
		bar = new JMenuBar();
		menu = new JMenu("菜單");
		item = new JMenuItem("查記錄");
		menu.add(item);
		bar.add(menu);
		//中間     游戲面板 和 記錄面板
		//中間是兩個卡片佈局  
		centerJPanel = new JPanel();
		centerJPanel.setLayout(new CardLayout());
		centerJPanel.add("玩游戲",new MouseJPanel(user));
		

		
		this.add(bar,BorderLayout.NORTH);
		this.add(centerJPanel);
		//綁定監聽器，實現面板的 切換
		item.addActionListener(this);
	}
	
	
	//實現面板切換的邏輯
	@Override
	public void actionPerformed(ActionEvent e) {
		//e.getActionCommand();
		String cardName = item.getText();
		//拿到卡片佈局
		CardLayout card = (CardLayout) centerJPanel.getLayout();
	      //展示卡片名字 - 根據菜單項
		if("玩游戲".equals(cardName)) {
			card.show(centerJPanel, cardName);
          item.setText("查記錄");			
		}else if ("查記錄".equals(cardName)) {
			centerJPanel.add("查記錄",new RecordJPanel(user));
			card.show(centerJPanel, cardName);
			item.setText("玩游戲");
		}
	}
}
